Factor search for sibling Cargo.toml and .git folder into separate subroutine.
authorFelix S. Klock II <pnkfelix@pnkfx.org>
Thu, 8 Jun 2017 10:39:28 +0000 (12:39 +0200)
committerFelix S. Klock II <pnkfelix@pnkfx.org>
Fri, 16 Jun 2017 15:59:05 +0000 (17:59 +0200)
src/cargo/sources/path.rs

index 442524df159f555ddd51e1956906419a15816d66..cb9c2dde07d7ccda4b5c1e0b8358d904888c9d35 100644 (file)
@@ -114,6 +114,20 @@ impl<'cfg> PathSource<'cfg> {
             }
         };
 
+        if let Some(result) = self.discover_git_and_list_files(pkg, root, &mut filter) {
+            return result;
+        }
+
+        self.list_files_walk(pkg, &mut filter)
+    }
+
+    // Returns Some(_) if found sibling Cargo.toml and .git folder;
+    // otherwise caller should fall back on full file list.
+    fn discover_git_and_list_files(&self,
+                                   pkg: &Package,
+                                   root: &Path,
+                                   filter: &mut FnMut(&Path) -> bool)
+                                   -> Option<CargoResult<Vec<PathBuf>>> {
         // If this package is in a git repository, then we really do want to
         // query the git repository as it takes into account items such as
         // .gitignore. We're not quite sure where the git repository is,
@@ -129,11 +143,14 @@ impl<'cfg> PathSource<'cfg> {
                 // check to see if we are indeed part of the index. If not, then
                 // this is likely an unrelated git repo, so keep going.
                 if let Ok(repo) = git2::Repository::open(cur) {
-                    let index = repo.index()?;
+                    let index = match repo.index() {
+                        Ok(index) => index,
+                        Err(err) => return Some(Err(err.into())),
+                    };
                     let path = util::without_prefix(root, cur)
                                     .unwrap().join("Cargo.toml");
                     if index.get_path(&path, 0).is_some() {
-                        return self.list_files_git(pkg, repo, &mut filter);
+                        return Some(self.list_files_git(pkg, repo, filter));
                     }
                 }
             }
@@ -146,7 +163,7 @@ impl<'cfg> PathSource<'cfg> {
                 None => break,
             }
         }
-        self.list_files_walk(pkg, &mut filter)
+        return None;
     }
 
     fn list_files_git(&self, pkg: &Package, repo: git2::Repository,